package com.mathijsjh.pwdhash;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.mathijsjh.pwdhash.pwdhash.PwdHash;
import com.mathijsjh.pwdhash.util.ClipboardHelper;

public class PwdHashApp extends Activity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Intent intent = getIntent();
        String domain = intent.getStringExtra(Intent.EXTRA_TEXT);
        
        
        setContentView(R.layout.main);
        
        EditText etDomain = (EditText) findViewById(R.id.etDomain);
        etDomain.setText(domain);
        
        
        Button button = (Button)findViewById(R.id.btGenerate);
        button.setOnClickListener(generateListener);
    }
    
    private OnClickListener generateListener = new OnClickListener() {
        public void onClick(View v) {
        	
        	EditText etDomain = (EditText) findViewById(R.id.etDomain);
        	EditText etPassword = (EditText) findViewById(R.id.etPassword);
        	
            String domain = etDomain.getText().toString();
            String password = etPassword.getText().toString();
            
            String passwordHash = PasswordHash(domain,password); 
            
            if (passwordHash != null)
            {
            	copyToClipboard(passwordHash);
            	finish();
            }
           
        }
    };
    	

    
    private String PasswordHash(String siteDomain, String sitePassword) {
        
        try {
                String domain = siteDomain;
                String pwdhash = PwdHash.GenerateHashedPassword(domain, sitePassword);
                
                return pwdhash;
                
        } catch (Throwable t) {
                        
        		Toast.makeText(this, t.getMessage().toString(), Toast.LENGTH_LONG).show();
                return null;
        }
    
    }
    
    private void copyToClipboard(String text)
    {
        try
        {
        	ClipboardHelper.Initialize(this);
            ClipboardHelper.setText(text);
           
            Toast.makeText(this, "Password hash is copied to the clipboard!", Toast.LENGTH_LONG).show();
            
        } catch (Throwable t) {

        	Toast.makeText(this, "Could not copy your password to the clipboard :-(", Toast.LENGTH_LONG).show();
                
        }       
    }
}